package cn.iocoder.yudao.module.product.controller.admin.recommend;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.recommend.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.recommend.RecommendDO;
import cn.iocoder.yudao.module.product.service.recommend.RecommendService;

@Tag(name = "管理后台 - 落地页SKU推荐")
@RestController
@RequestMapping("/product/recommend")
@Validated
public class RecommendController {

    @Resource
    private RecommendService recommendService;

    @PostMapping("/create")
    @Operation(summary = "创建落地页SKU推荐")
    @PreAuthorize("@ss.hasPermission('product:recommend:create')")
    public CommonResult<Long> createRecommend(@Valid @RequestBody RecommendSaveReqVO createReqVO) {
        return success(recommendService.createRecommend(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新落地页SKU推荐")
    @PreAuthorize("@ss.hasPermission('product:recommend:update')")
    public CommonResult<Boolean> updateRecommend(@Valid @RequestBody RecommendSaveReqVO updateReqVO) {
        recommendService.updateRecommend(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除落地页SKU推荐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:recommend:delete')")
    public CommonResult<Boolean> deleteRecommend(@RequestParam("id") Long id) {
        recommendService.deleteRecommend(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得落地页SKU推荐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:recommend:query')")
    public CommonResult<RecommendRespVO> getRecommend(@RequestParam("id") Long id) {
        RecommendDO recommend = recommendService.getRecommend(id);
        return success(BeanUtils.toBean(recommend, RecommendRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得落地页SKU推荐分页")
    @PreAuthorize("@ss.hasPermission('product:recommend:query')")
    public CommonResult<PageResult<RecommendRespVO>> getRecommendPage(@Valid RecommendPageReqVO pageReqVO) {
        PageResult<RecommendDO> pageResult = recommendService.getRecommendPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecommendRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出落地页SKU推荐 Excel")
    @PreAuthorize("@ss.hasPermission('product:recommend:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecommendExcel(@Valid RecommendPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecommendDO> list = recommendService.getRecommendPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "落地页SKU推荐.xls", "数据", RecommendRespVO.class,
                        BeanUtils.toBean(list, RecommendRespVO.class));
    }

}